package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.YhqMyAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class LotteryDetailActivity extends BaseActivity {

    private Context context = this;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.yxq)
    TextView yxq;
    @BindView(R.id.num)
    TextView num;

    private String M_KEY;
    private String ltty_act_id;
    private String TITLE;
    private int numInt = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_lottery_detail;
    }

    @Override
    protected void initView() {
        M_KEY = getIntent().getStringExtra("id");
        findViewById(R.id.ljcjBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, LotteryWebViewActivity.class).putExtra("TITLE", TITLE)
                        .putExtra("url", HttpAdapter.HEAD_URL+"unlimited/draw/goview.do?TOKEN="+BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN)+"&ACT_ID="+ltty_act_id)
                );
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        cpActInfo();
    }

    private void cpActInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().cpActInfo(M_KEY, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    if(linkedTreeMap.containsKey("ltty_act_id")){
                        ltty_act_id = decimalFormat.format(linkedTreeMap.get("ltty_act_id"));
                    }
                    if (linkedTreeMap.containsKey("num")){
                        numInt = Integer.parseInt(decimalFormat.format(linkedTreeMap.get("num")));
                        num.setText((decimalFormat.format(linkedTreeMap.get("num")))+"次");
                    }

                    if (numInt == 0){
                        findViewById(R.id.ljcjBtn).setVisibility(View.GONE);
                    }else{
                        findViewById(R.id.ljcjBtn).setVisibility(View.VISIBLE);
                    }
                    TITLE = linkedTreeMap.get("title")+"";
                    title.setText(linkedTreeMap.get("title")+"");

                    yxq.setText(linkedTreeMap.get("coupon_use_beg")+" 至 "+linkedTreeMap.get("coupon_use_end"));
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
